package com.mediacloud.app.newsmodule.fragment.topic;


import android.util.Log;
import android.view.View;
import android.widget.AdapterView;

import com.mediacloud.app.assembly.banner.NetImageBanner;
import com.mediacloud.app.model.news.ArticleItem;
import com.mediacloud.app.model.news.ArticleListData;
import com.mediacloud.app.model.news.BaseMessageReciver;
import com.mediacloud.app.model.news.kt.TypeX;
import com.mediacloud.app.model.view.floatwin.FloatButtonGroup;
import com.mediacloud.app.newsmodule.adaptor.NewsListItemStyleAdaptor;
import com.mediacloud.app.newsmodule.adaptor.bigimg_nosliceline.BigImageNoSliceLineAdaptor;
import com.mediacloud.app.newsmodule.adaptor.topic.TopicNewsListItemStyleAdaptor;
import com.mediacloud.app.newsmodule.fragment.BaseNewsListFragment;
import com.mediacloud.app.newsmodule.fragment.newslist.NewsListWidthBannerFragment;
import com.mediacloud.app.newsmodule.utils.CacheDataKt;
import com.mediacloud.app.newsmodule.utils.NewsItemClickUtils;
import com.mediacloud.app.newsmodule.utils.ThemeKit;
import com.mediacloud.app.reslib.enums.StyleOther;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * 专题页
 */
public class TopicListFragment extends NewsListWidthBannerFragment {
    public TopicListFragment() {
    }

    @Override
    public void initView() {
        super.initView();

    }

    @Override
    public void beforeUserCache() {
        super.beforeUserCache();
        if (adaptor instanceof NewsListItemStyleAdaptor){
            ((NewsListItemStyleAdaptor) adaptor).destroy();
        }
        if (adaptor instanceof BigImageNoSliceLineAdaptor){
            ((BigImageNoSliceLineAdaptor) adaptor).destroy();
        }
        //如果有其他样式的 就用这个列表样式了
        if (catalogItem.getCatalogStyle() == StyleOther.OtherStyle)//&&catalogItem.getStyleOther()!=null)
        {
            adaptor = new BigImageNoSliceLineAdaptor(getActivity(), catalogItem, playClickListener);
        }else{
            adaptor = new TopicNewsListItemStyleAdaptor(getActivity(), catalogItem);
        }
        mCatalogContentItemListView.setAdapter(adaptor);
    }

    @Override
    public void onDestroyView() {
        if (adaptor instanceof TopicNewsListItemStyleAdaptor) {
            ((TopicNewsListItemStyleAdaptor) adaptor).clearEvent();
            ((TopicNewsListItemStyleAdaptor) adaptor).clearSpiderObservers();
        }
        super.onDestroyView();
    }
    @Override
    protected void addBanner() {
        int extraStyle = catalogItem != null ? catalogItem.getCatalogStyle() : 0;
        if (extraStyle > 0)
            haveBanner = false;
        else
            super.addBanner();
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position,
                            long id) {
        int index = position;
        index = index - mCatalogContentItemListView.getHeaderViewsCount();
        if (index < 0)
            return;
        ArticleItem item = (ArticleItem) adaptor.getItem(index);
        item.setParentId(null);
        if (item.getType() != TypeX.CUSTOM.ADVERT)
            item.setType(TypeX.NEWS.TOPIC);
        NewsItemClickUtils.OpenItemNewsHandle(getActivity(), item.getType(), item, catalogItem);
    }
    @Override
    public void onClick(NetImageBanner banner, int position, Object tag) {
        ArticleItem item = bannerData.get(position);
//		item.setParentId(catalogID);
        NewsItemClickUtils.OpenItemNewsHandle(getActivity(), item.getType(), item, catalogItem);
    }

    protected Class<? extends BaseDataInvokeCallBack<? extends BaseMessageReciver>> getDataInvokeCallBackClass() {
        return TopicListInvokeCallBack.class;
    }

    public class TopicListInvokeCallBack extends BaseDataInvokeCallBack<ArticleListData> {
        @Override
        public void success(ArticleListData result) {
            if (getActivity() == null || requireActivity().isFinishing()||requireActivity().isDestroyed()) {
                return ;
            }
            Log.w("APPTAG", "success topiclist");
            if (adaptor == null)
                return;
            boolean isCacheData = false;
            if (pageNum == 1 && !result.haveMore() &&
                    result.articleList != null && result.articleList.size() == 0) {
                ArticleItem footerEmptyArticleItem = new ArticleItem();
                footerEmptyArticleItem.setType(TypeX.CUSTOM.Footer_Empty);
                result.articleList.add(footerEmptyArticleItem);
                isAddFooterEmptyArticleItem = true;
            }
            if (needCache() && pageNum == 1) {
                //不是上次的缓存数据才进行缓存
                isCacheData = result.orginData.optInt("is_cache",0)==1;
                if(!isCacheData){
                    try {
                        result.orginData.put("is_cache",1);
                        CacheDataKt.saveData(TopicListFragment.this, catalogID, result.orginData + "");
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            }
            isLoading = false;
            JSONObject data = result.orginData.optJSONObject("data");
            if (pageNum == 1) {
                if (FloatButtonGroup.haveFloatField(data)) {
                    initFloatWin(data);
                } else {
                    hideFloatWin();
                }
            }
            for (ArticleItem articleItem : result.articleList) {
                if (articleItem.getType() != TypeX.CUSTOM.ADVERT)
                    articleItem.setType(TypeX.NEWS.TOPIC);
            }
//			mCatalogContentItemListView.setPullLoadEnable(result.more);
            if (pageNum == 1) {
                mXSmartRefreshLayout.finishRefresh();
                if (needAddComponent31(result.componentItems) && haveBanner) {
                    addComponent31Header(result);
                }
               // componetContainer.updateComponent(result.componentItems);
                for (int i = 0; i < result.componentItems.size(); i++) {
                    result.componentItems.get(i).belongNavId = catalogID;
                }
                    bufferComponentContainer.setData(result.componentItems,catalogItem);
//				if(haveBanner&&result.articleList.size()>maxBannerSize)
                if (haveBanner && !needRemoveSelfBanner(result.componentItems)) {
                    bannerData.clear();
                    int maxSize = result.articleList.size() >= maxBannerSize ? maxBannerSize : result.articleList.size();
                    for (int i = 0; i < maxSize; i++) {
                        if (maxSize==1&&result.articleList.get(i).getType()==TypeX.CUSTOM.Footer_Empty){
                            break;
                        }
                        bannerData.add(result.articleList.remove(0));
                    }
                    attached = true;
                    refreshBanner();
                }
//                else if (banner != null) {
//                    banner.defaultHeight = 0;
//                    banner.cleanAll();
//                    banner.updateLayout();
//                }
                if (adaptor.getListContentData() != null)
                    adaptor.getListContentData().clear();
            }
            if (result.more)
                mXSmartRefreshLayout.finishLoadMore();
            else
                mXSmartRefreshLayout.finishLoadMoreWithNoMoreData();
            if (adaptor.getListContentData() == null)
                adaptor.setListContentData(result.articleList);
            else
                adaptor.getListContentData().addAll(
                        result.articleList);
//			adaptor.getListContentData().addAll(0, result.componentList);
            adaptor.notifyDataSetChanged();
/*			mCatalogContentItemListView.stopLoadMore();
			mCatalogContentItemListView.stopRefresh();*/
//			if (bannerData.size()==0&&
            if (isAddFooterEmptyArticleItem){
                if (bannerData.size() == 0 && (adaptor.getListContentData() == null || adaptor.getListContentData().size() <= 1) && result.componentList.size() == 0) {
                    showStateView(TYPE_NO_CONTENT, false);
                } else {
                    closeStateView();
                }
            }else {
                if (bannerData.size() == 0 && (adaptor.getListContentData() == null || adaptor.getListContentData().size() == 0) && result.componentList.size() == 0) {
                    showStateView(TYPE_NO_CONTENT, false);
                } else {
                    closeStateView();
                }
            }
            if(isCacheData){
                mXSmartRefreshLayout.autoRefresh(400);
            }
        }
    }
}